package com.xiaoyang.common.message.sender;

import org.springframework.stereotype.Controller;

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.BufferedReader;
import java.io.InputStreamReader;

@Controller
public class SMSSender {
    public static String sendSMS(String templateId, String code, String phone) throws Exception {
        String url = String.format("https://push.spug.cc/send/%s?code=%s&targets=%s",
            templateId, code, phone);

        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        
        return response.toString();
    }

    public static void main(String[] args) {
        try {
            String result = sendSMS("k3njZeBAmM4D", "6677", "151xxxx0875");
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
